﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.BasicData.Customers;
using mozhi.smarterp.BasicData.Customers.Repositories;
using mozhi.smarterp.BasicData.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.BasicData.Repositories
{
    public class CustomerRepository : EfCoreRepository<BasicDataDbContext, Customer, Guid>, ICustomerRepository
    {
        public CustomerRepository(IDbContextProvider<BasicDataDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<int> GetCountAsync(string code, string name, Guid? excludeId, CancellationToken cancellationToken = default(CancellationToken))
        {
            var dbContext = await GetDbContextAsync();

            int count = await dbContext.Set<Customer>().AsNoTracking()
                .WhereIf(!code.IsNullOrEmpty(), x => x.Code == code)
                .WhereIf(!name.IsNullOrEmpty(), x => x.Name == name)
                .WhereIf(excludeId.HasValue, x => x.Id != excludeId)
                .CountAsync(cancellationToken);

            return count;
        }
    }
}
